<template>
    <div>
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">
                首页</el-breadcrumb-item
            >
            <el-breadcrumb-item :to="{ path: '/banner/List' }">
                内容管理</el-breadcrumb-item
            >
            <el-breadcrumb-item>栏目管理</el-breadcrumb-item>
        </el-breadcrumb>
        <el-card class="box-card">
            <el-row>
                <el-col :span="2">
                    <el-tooltip
                        :enterable="false"
                        effect="dark"
                        content="添加栏目"
                        placement="top"
                    >
                        <el-button type="primary" @click="add">
                            <i class="el-icon-circle-plus-outline"></i>
                            添加栏目
                        </el-button>
                    </el-tooltip>
                </el-col>
                <el-col :span="10"
                    ><div class="grid-content bg-purple"></div
                ></el-col>
                <el-col :span="8"
                    ><div class="grid-content bg-purple"></div
                ></el-col>
            </el-row>

            <el-table :data="list" border stripe style="width: 100%">
                <el-table-column type="index" label="#" />
                <el-table-column prop="name" label="栏目名称" />
                <el-table-column label="栏目类型" width="150">
                    <template v-slot="scope">
                        <el-button
                            type="success"
                            v-if="scope.row.target_type == 1"
                            >文章分类</el-button
                        >
                        <el-button type="info" v-if="scope.row.target_type == 2"
                            >文章</el-button
                        >
                        <el-button
                            type="warning"
                            v-if="scope.row.target_type == 3"
                            >单页模板名称</el-button
                        >
                        <el-button
                            type="danger"
                            v-if="scope.row.target_type == 4"
                            >外链</el-button
                        >
                    </template>
                </el-table-column>
                <el-table-column prop="sort" label="排序" width="80" />
                <el-table-column
                    prop="create_time"
                    label="创建时间"
                    width="180"
                />
                <el-table-column
                    prop="update_time"
                    label="更新时间"
                    width="180"
                />
                <el-table-column label="状态" width="70">
                    <template v-slot="scope">
                        <el-switch
                            v-model="scope.row.status"
                            :active-value="1"
                            :inactive-value="0"
                            @change="updateStatus(scope.row)"
                        />
                    </template>
                </el-table-column>
                <el-table-column label="操作">
                    <template v-slot="scope">
                        <el-tooltip
                            :enterable="false"
                            effect="dark"
                            content="编辑栏目信息"
                            placement="top"
                        >
                            <el-button
                                type="primary"
                                size="mini"
                                @click="edit(scope.row.id)"
                            >
                                <i class="el-icon-edit"></i>
                            </el-button>
                        </el-tooltip>
                        <el-tooltip
                            :enterable="false"
                            effect="dark"
                            content="删除栏目"
                            placement="top"
                        >
                            <el-button
                                type="danger"
                                size="mini"
                                @click="del(scope.row.id)"
                            >
                                <i class="el-icon-delete"></i>
                            </el-button>
                        </el-tooltip>
                    </template>
                </el-table-column>
            </el-table>
            <el-config-provider :locale="locale">
                <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="form_data.page"
                    :page-sizes="[20, 50, 100, 150]"
                    :page-size="form_data.limit"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total"
                >
                </el-pagination>
            </el-config-provider>
        </el-card>
    </div>
</template>

<script>
import requestApis from '@/request'
import zhCn from 'element-plus/lib/locale/lang/zh-cn'

export default {
    data () {
        return {
            locale: zhCn,
            list: [],
            total: 0,
            form_data: {
                keyword: '',
                page: 1,
                limit: 20
            }
        }
    },
    created () {
        this.getColumnList()
    },
    methods: {
        add () {
            this.$router.push('/column/Add')
        },
        edit (id) {
            this.$router.push({ path: '/column/Edit', query: { id: id } })
        },
        del (id) {
            this.$confirm('此操作将永久删除这条记录, 是否继续?', '删除确认', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                requestApis.saveColumnDel({ id }).then(res => {
                    if (res.meta.code === 1) {
                        this.$message({
                            type: 'success',
                            message: res.meta.msg
                        })
                        this.getColumnList()
                    } else {
                        this.$message({
                            message: res.meta.msg,
                            type: 'warning'
                        })
                    }
                }).catch(error => {
                    console.log(error)
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                })
            })
        },
        updateStatus (row) {
            requestApis.saveColumnEdit({ id: row.id, status: row.status }).then(res => {
                if (res.meta.code === 1) {
                    this.getColumnList()
                    this.$message({
                        message: res.meta.msg,
                        type: 'success'
                    })
                } else {
                    this.$message({
                        message: res.meta.msg,
                        type: 'warning'
                    })
                }
            }).catch(error => {
                console.log(error)
            })
        },
        setLimit (curLimit) {
            this.form_data.limit = curLimit
        },
        getColumnList () {
            requestApis.getColumnList(this.form_data).then(res => {
                if (res.meta.code === 1) {
                    this.list = res.data.list
                    this.total = res.data.total
                } else {
                    this.$message({
                        message: res.meta.msg,
                        type: 'warning'
                    })
                }
            }).catch(error => {
                console.log(error)
            })
        },
        handleSizeChange (newLimit) {
            this.form_data.limit = newLimit
            this.getColumnList()
        },
        handleCurrentChange (newPage) {
            this.form_data.page = newPage
            this.getColumnList()
        }
    }

}
</script>

<style lang="stylus" scoped>
.text {
    font-size: 14px;
}

.el-icon-search {
    font-size: 20px;
}

.item {
    padding: 18px 0;
}

.el-table {
    margin-top: 15px;
}

.el-pagination {
    margin-top: 15px;
}

.page_icon {
    font-size: 30px;
}

.input-with-select .el-input-group__prepend {
    background-color: #fff;
}
</style>
